package com.huang.A01_case;

import java.util.Random;
import java.util.Scanner;

/**
 * 目标：
 * 实现双色球
 */
public class Demo9 {
    public static void main(String[] args) {
        int[] userNumbers = userSelectNumbers();
        System.out.println("您投注的号码为：");
        print(userNumbers);
        int[] luckyNumbers = createNumbers();
        System.out.println("中奖号码为：");
        print(luckyNumbers);
        judge(userNumbers,luckyNumbers);
    }

    //打印数组
    public static void print(int[] numbers) {
        System.out.print("[");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(i == numbers.length - 1 ? numbers[i] : numbers[i] + ", ");
        }
        System.out.println("]");
    }

    //用户选号
    public static int[] userSelectNumbers() {
        //定义动态初始化数组 6个红色球号码，1个蓝色号码
        int[] numbers = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < numbers.length - 1; i++) {
            //用户输入6个蓝色号码
            while (true) {
                System.out.println("请您输入第" + (i + 1) + "个蓝色号码");
                int number = sc.nextInt();
                //判断输入的号码是否合法 1-33 不重复
                if (number < 1 || number > 33) {
                    System.out.println("对不起，您输入的号码不在范围内，请重新输入");
                } else {
                    //重复
                    if (exist(numbers, number)) {
                        System.out.println("对不起，您输入的号码重复，请重新输入");
                    } else {
                        //合法
                        numbers[i] = number;
                        //跳出循环
                        break;
                    }
                }
            }
        }
        //生成蓝色号码 1-16
        while (true) {
            System.out.println("请输入最后一个蓝色号码");
            int number = sc.nextInt();
            if (number < 1 || number > 16) {
                System.out.println("对不起，您输入的蓝色号码不在范围内，请重新输入");
            } else {
                //合法
                numbers[6] = number;
                //结束循环
                break;
            }
        }
        return numbers;
    }

    //判断号码是否重复
    public static boolean exist(int[] numbers, int number) {
        //遍历数组 如果遍历到0,说明号码已经比对完，没有重复，直接返回false
        // [1,2,3,4,0,0]  5
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == 0) {
                return false;
            }
            //相等则表示重复了
            if (numbers[i] == number) {
                return true;
            }
        }
        //如果循环结束都没有 找出重复号码表示没有重复
        return false;
    }

    //随机一组中奖号码
    public static int[] createNumbers() {
        //定义动态数组存放中将号码
        int[] numbers = new int[7];
        Random r = new Random();
        //6个红色号码
        for (int i = 0; i < numbers.length - 1; i++) {
            while (true) {
                //生成随机号码 1-33 -> (0-32) + 1
                int number = r.nextInt(33) + 1;
                //号码不重复
                if (!exist(numbers, number)) {
                    numbers[i] = number;
                    break;//将不重复的号码赋值给当前位置，结束死循环
                }
            }
        }
        //1个蓝色号码 1-16 ->（0-15）+1
        int number = r.nextInt(16) + 1;
        numbers[6] = number;
        return numbers;
    }

    //判断中奖情况
    public static void judge(int[] userNumbers, int[] luckyNumbers) {
        //记录红球、蓝球中奖数量
        int redCount = 0;
        int blueCount = 0;

        //比对红球中奖数量
        for (int i = 0; i < userNumbers.length - 1; i++) {
            for (int j = 0; j < luckyNumbers.length - 1; j++) {
                if (userNumbers[i] == luckyNumbers[i]) {
                    redCount++;
                    break;
                }
            }
        }
        //比对蓝球中奖数量
        blueCount = userNumbers[6] == luckyNumbers[6] ? 1 : 0;
        System.out.println("红球命中数量为：" + redCount);
        System.out.println("蓝球命中数量为：" + blueCount);
        //判断中奖情况
        if (redCount == 6 && blueCount == 1) {
            System.out.println("恭喜您，中奖1000万");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("恭喜您，中奖500万");
        } else if (redCount == 5 && blueCount == 1) {
            System.out.println("恭喜您，中奖3000元");
        } else if (redCount == 5 && blueCount == 0 || redCount == 4 && blueCount == 1) {
            System.out.println("恭喜您，中奖200元");
        } else if (redCount == 4 && blueCount == 0 || redCount == 3 && blueCount == 1) {
            System.out.println("恭喜您，中奖10元");
        } else if (redCount < 3 && blueCount == 1) {
            System.out.println("恭喜您，中奖5元");
        } else {
            System.out.println("谢谢参与~~~");
        }
    }
}